package com.yc.psychologicalconsultant.controller;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.yc.psychologicalconsultant.dao.DigitalPeopleDAO;
import com.yc.psychologicalconsultant.pojo.Result;
import com.yc.psychologicalconsultant.service.UserService;
import com.yc.psychologicalconsultant.utils.AliOSSUtils;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@CrossOrigin
@Slf4j
@RestController
public class UploadController {
    @Resource
    private UserService userService;
    @Resource
    private AliOSSUtils aliOSSUtils;
    @Autowired
    DigitalPeopleDAO digitalPeopleDAO;

    @ApiOperation("更新头像")
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile image
    , @RequestParam(value = "id",required = false) String id) throws IOException {
        System.out.println(image.getOriginalFilename());
        log.info("文件上传 {} {}",image.getOriginalFilename(),id);
        String upload = aliOSSUtils.upload(image);
        log.info("文件访问的url: {}",upload);
        try {
            userService.updateAvatar(id,upload);
        }catch (Exception e){
         return Result.error("上传失败");
        }
        return Result.success(upload);
    }
    @PostMapping("/uploadDigitalPeopleImg")
    public Result uploadDigitalPeople(@RequestParam("file") MultipartFile image
            , @RequestParam(value = "name",required = false) String name,@RequestParam(value = "description",required = false) String description) throws IOException {
        System.out.println(image.getOriginalFilename());
        String upload = aliOSSUtils.upload(image);
        log.info("文件上传 {} {} {}",image.getOriginalFilename(),name,description);
        File file=new File("E:\\SadTalker\\resource",image.getOriginalFilename());
        image.transferTo(file);
        String img="resource/"+image.getOriginalFilename();
        digitalPeopleDAO.InsertDigitalPeopleImg(img,upload,name,description);
//        log.info("文件访问的url: {}",upload);
//        return Result.success(upload);
        return Result.success(upload);
    }
    @PostMapping("/uploadDigitalPeopleWav")
    public Result uploadDigitalPeopleWav(@RequestParam("file") MultipartFile wav
            , @RequestParam(value = "name",required = false) String name) throws IOException {
        System.out.println(wav.getOriginalFilename());
        File file=new File("E:\\GPT-SoVITS\\GPT-SoVITS-beta0306fix",wav.getOriginalFilename());
        wav.transferTo(file);
        digitalPeopleDAO.InsertDigitalPeopleWav(wav.getOriginalFilename(),name);
        log.info("文件上传 {} {}",wav.getOriginalFilename(),name);

        return Result.success();
    }
}
